<template>
  <div>
    <a-card :bordered="false" :bodyStyle="tstyle">
      <div class="table-page-search-wrapper" v-if="hasPerm('timeSpaceDataRecord:page')">
        <a-form layout="inline">
          <a-row :gutter="48">
            <a-col :md="8" :sm="24">
              <a-form-item label="文件名称">
                <a-input v-model="queryParam.title" allow-clear placeholder="请输入文件名称"/>
              </a-form-item>
            </a-col>
            <a-col :md="8" :sm="24">
              <a-form-item label="数据类型">
                <a-select v-model="queryParam.typeCode" allow-clear placeholder="请选择数据类型">
                  <a-select-option
                    v-for="item in dataType"
                    :key="item.code"
                    :value="item.code"
                  >
                    {{ item.name }}
                  </a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
<!--            <template v-if="advanced">-->
<!--            </template>-->
            <a-col :md="8" :sm="24" >
              <span class="table-page-search-submitButtons">
                <a-button type="primary" @click="$refs.table.refresh(true)" >查询</a-button>
                <a-button style="margin-left: 8px" @click="() => queryParam = {}">重置</a-button>
<!--                <a @click="toggleAdvanced" style="margin-left: 8px">-->
<!--                  {{ advanced ? '收起' : '展开' }}-->
<!--                  <a-icon :type="advanced ? 'up' : 'down'"/>-->
<!--                </a>-->
              </span>
            </a-col>
          </a-row>
        </a-form>
      </div>
    </a-card>
    <a-card :bordered="false">
      <s-table
        ref="table"
        :columns="columns"
        :data="loadData"
        :rowKey="(record) => record.id"
      >
        <template class="table-operator" slot="operator" v-if="hasPerm('timeSpaceDataRecord:add')" >
          <a-button type="primary" v-if="hasPerm('timeSpaceDataRecord:add')" icon="plus" @click="$refs.uploadModal.init()">新增</a-button>
          <a-button type="primary" icon="tag" @click="$refs.dataType.init()">数据类型</a-button>
          <a-button icon="export" @click="$refs.exportForm.init()">导出</a-button>
        </template>

        <template slot="dataSize" slot-scope="text">
          <span>{{ text !== null && text != 0 ? text + ' 条' : '--' }}</span>
        </template>

        <template slot="typeCode" slot-scope="text">
          <span v-if="text === null">--</span>
          <span v-else>{{ dataTypeMap[text].name }}</span>
        </template>

        <template slot="status" slot-scope="text">
          <span v-if="text == 0"><a-tag color="#2db7f5">已上传</a-tag></span>
          <span v-if="text == 2"><a-tag color="#87d068">解析中</a-tag></span>
          <span v-if="text == 3"><a-tag color="#108ee9">解析完成</a-tag></span>
          <span v-if="text == 4"><a-tag color="#f50">解析失败</a-tag></span>
          <span v-if="text == 5"><a-tag color="#87d068">导入中</a-tag></span>
          <span v-if="text == 6"><a-tag color="#f50">导入失败</a-tag></span>
          <span v-if="text == 7"><a-tag color="#29c15f">导入完成</a-tag></span>
        </template>

        <span slot="action" slot-scope="text, record">
          <a v-if="record.status == 0 || record.status == 4" @click="doParse(record)">解析</a>
          <a v-if="record.status == 3 || record.status == 6 || record.status == 7" @click="$refs.importData.init(record)">{{ record.status == 3 ? '导入' : '重新导入' }}</a>
          <span v-if="record.status == 2 || record.status == 5" style="padding: 0 7px;">
            <a-icon style="color: #87d068;"  type="loading-3-quarters" spin/>
          </span>
          <a-divider type="vertical" />
          <a @click="toDetail(record)">查看</a>
          <!--          <a-divider type="vertical" />-->
          <!--          <a v-if="hasPerm('timeSpaceDataRecord:edit')" @click="$refs.editForm.edit(record)">编辑</a>-->
          <a-divider type="vertical" v-if="hasPerm('timeSpaceDataRecord:edit') & hasPerm('timeSpaceDataRecord:delete')"/>
          <a-popconfirm v-if="hasPerm('timeSpaceDataRecord:delete')" placement="topRight" title="确认删除？" @confirm="() => singleDelete(record)">
            <a>删除</a>
          </a-popconfirm>
        </span>
      </s-table>
      <add-form ref="addForm" @ok="handleOk" />
      <edit-form ref="editForm" @ok="handleOk" />
      <upload-modal ref="uploadModal" @ok="handleOk" @parseComplete="handleOk"/>
      <detail-table ref="detailTable"/>
      <data-type ref="dataType" @success="handleDataType"/>
      <import-data ref="importData" @ok="handleOk"/>
      <export-form ref="exportForm"/>
    </a-card>
  </div>
</template>

<script>
  import { STable } from '@/components'
  import _ from 'lodash'
  import {
    timeSpaceDataRecordPage,
    timeSpaceDataRecordDelete,
    timeSpaceDataRecordExport,
    parseFile,
    timeSpaceDataRecordDeleteOne
  } from '@/api/modular/main/tsdatarecord/timeSpaceDataRecordManage'
  import addForm from './addForm.vue'
  import editForm from './editForm.vue'
  import uploadModal from '@/views/main/tsdatarecord/modal/uploadModal'
  import detailTable from '@/views/main/tsdatarecord/detailTable'
  import { Empty } from 'ant-design-vue'
  import dataType from '@/views/main/tsdatarecord/dataType'
  import importData from "@/views/main/tsdatarecord/importData";
  import exportForm from "@/views/main/tsdatarecord/exportForm";
  import {baseDataTypeList} from "@/api/modular/main/tsdatarecord/baseDataType";
  export default {
    components: {
      STable,
      addForm,
      editForm,
      uploadModal,
      detailTable,
      dataType,
      importData,
      exportForm
    },
    data () {
      return {
        // 高级搜索 展开/关闭
        advanced: false,
        // 查询参数
        queryParam: {},
        // 表头
        columns: [
          {
            title: '文件名称',
            align: 'center',
            dataIndex: 'title'
          },
          {
            title: '文件大小',
            align: 'center',
            dataIndex: 'sizeInfo'
          },
          {
            title: '数据条数',
            align: 'center',
            dataIndex: 'dataSize',
            scopedSlots: { customRender: 'dataSize' }
          },
          {
            title: '数据类型',
            align: 'center',
            dataIndex: 'typeCode',
            scopedSlots: { customRender: 'typeCode' }
          },
          {
            title: '上传时间',
            align: 'center',
            dataIndex: 'createTime'
          },
          {
            title: '状态',
            align: 'center',
            dataIndex: 'status',
            scopedSlots: { customRender: 'status' }
          }
        ],
        tstyle: { 'padding-bottom': '0px', 'margin-bottom': '10px' },
        // 加载数据方法 必须为 Promise 对象
        loadData: parameter => {
          return timeSpaceDataRecordPage(Object.assign(parameter, this.queryParam)).then((res) => {
            return res.data
          })
        },
        selectedRowKeys: [],
        selectedRows: [],
        options: {
          alert: { show: true, clear: () => { this.selectedRowKeys = [] } },
          rowSelection: {
            selectedRowKeys: this.selectedRowKeys,
            onChange: this.onSelectChange
          }
        },
        folderTreeData: [],
        simpleImage: Empty.PRESENTED_IMAGE_SIMPLE,
        defaultExpandedKeys: [],
        replaceFields: {
          key: 'id',
          title: 'name'
        },
        dataType: [],
        dataTypeMap: {}
      }
    },
    created () {
      if (this.hasPerm('timeSpaceDataRecord:edit') || this.hasPerm('timeSpaceDataRecord:delete')) {
        this.columns.push({
          title: '操作',
          width: '180px',
          dataIndex: 'action',
          scopedSlots: { customRender: 'action' }
        })
      }
      this.loadDataType()
    },
    methods: {
      handleDataType() {
        this.loadDataType()
        this.handleOk()
      },
      loadDataType() {
        baseDataTypeList().then(res => {
          if (res.success) {
            this.dataType = res.data
            this.dataTypeMap = _.keyBy(res.data, 'code')
          }
        })
      },
      handleFolderTreeClick(data) {

      },
      toDetail(record) {
        this.$refs.detailTable.init(record)
      },
      doParse(record) {
        parseFile(record).then(r => {
          if (r.success) {
            this.$refs.table.refresh()
            this.$message.success('正在解析，请稍后刷新！')
          } else {
            this.$refs.table.refresh()
            this.$message.warning('解析失败！')
          }
        }).catch(() => {
          this.$refs.table.refresh()
          this.$message.warning('解析失败！')
        })
      },
      /**
       * 单个删除
       */
      singleDelete (record) {
        timeSpaceDataRecordDeleteOne(record.id).then(res => {
          if (res.success) {
            this.$message.success('删除成功')
            this.$refs.table.clearRefreshSelected()
          } else {
            this.$message.error('删除失败') // + res.message
          }
        })
      },
      /**
       * 批量删除
       */
      batchDelete () {
        const paramIds = this.selectedRowKeys.map((d) => {
            return { 'id': d }
        })
        this.timeSpaceDataRecordDelete(paramIds)
      },
      timeSpaceDataRecordDelete (record) {
        timeSpaceDataRecordDelete(record).then((res) => {
          if (res.success) {
            this.$message.success('删除成功')
            this.$refs.table.clearRefreshSelected()
          } else {
            this.$message.error('删除失败') // + res.message
          }
        })
      },
      toggleAdvanced () {
        this.advanced = !this.advanced
      },
      /**
       * 批量导出
       */
      batchExport () {
        const paramIds = this.selectedRowKeys.map((d) => {
            return { 'id': d }
        })
        timeSpaceDataRecordExport(paramIds).then((res) => {
            this.$refs.batchExport.downloadfile(res)
        })
      },
      handleOk () {
        this.$refs.table.refresh()
      },
      onSelectChange (selectedRowKeys, selectedRows) {
        this.selectedRowKeys = selectedRowKeys
        this.selectedRows = selectedRows
      }
    }
  }
</script>
<style lang="less">
  .table-operator {
    margin-bottom: 18px;
  }
  button {
    margin-right: 8px;
  }
</style>
